<?php
class PackageController {
	public function __construct() {
		header('Content-type: text/html; charset=utf-8');
	}
	public function addAction() {
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			$time = microtime(TRUE);
			if (is_uploaded_file($_FILES['file']['tmp_name'])) {
				$fullPath = ROOT . 'data/temp/' . $time . '.zip';
				move_uploaded_file($_FILES['file']['tmp_name'], $fullPath);
			}
			$result = PackageModel::add($fullPath);
			if (is_numeric($result)) {
				//出错
				echo '<h1>添加失败</h1>';
				switch ($result) {
					case PackageModel::ERR_FILE:
						echo '<p>打开文件失败</p>';
						break;
					case PackageModel::ERR_JSON:
						echo '<p>xmp.config.json不是有效的配置文件<p>';
						break;
					case PackageModel::ERR_NAME:
						echo '<p>名称不能为空</p>';
						break;
					case PackageModel::ERR_VER:
						echo '<p>版本号非法</p>';
						break;
				}
				@unlink($fullPath);
			} else {
				echo '<p>添加成功</p>';
				echo '<p>ID: ', $result, '</p>';
			}
			echo '<p><a href="javascript:history.go(-1);">返回</a></p>';
		} else {
			include(ROOT . 'modules/admin/views/package/add.phtml');
		}
	}
	public function updateAction() {
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			$time = microtime(TRUE);
			if (is_uploaded_file($_FILES['file']['tmp_name'])) {
				$fullPath = ROOT . 'data/temp/' . $time . '.zip';
				move_uploaded_file($_FILES['file']['tmp_name'], $fullPath);
			}
			$result = PackageModel::add($fullPath);
			if (is_numeric($result)) {
				//出错
				echo '<h1>添加失败</h1>';
				switch ($result) {
					case PackageModel::ERR_FILE:
						echo '<p>打开文件失败</p>';
						break;
					case PackageModel::ERR_JSON:
						echo '<p>xmp.config.json不是有效的配置文件<p>';
						break;
					case PackageModel::ERR_NAME:
						echo '<p>名称不能为空</p>';
						break;
					case PackageModel::ERR_VER:
						echo '<p>版本号非法</p>';
						break;
				}
				@unlink($fullPath);
			} else {
				echo '<p>添加成功</p>';
				echo '<p>ID: ', $result, '</p>';
			}
			echo '<p><a href="javascript:history.go(-1);">返回</a></p>';
		} else {
			include(ROOT . 'modules/admin/views/package/update.phtml');
		}
	}
}
